@page "/ai/config"
@using BootBlazor.Servers.Auth
@using BootBlazor.Servers.Components.Pages
@using BootBlazor.Servers.Helper
@using BootBlazor.Servers.Data
@using BootBlazor.Servers.Services
@rendermode RenderMode.InteractiveServer
@attribute [StreamRendering]

<PageHeader Title="@Loc["AIConfigPage_Title"]">
    <MudGrid Spacing="2" Class="justify-end">
        <MudItem xs="12" sm="6" md="3" lg="2" xl="1">
            <MudTextField T="string" @bind-Value="searchObject.SearchConfigName"
                          Margin="Margin.Dense" Label="@Loc["AIConfigPage_SearchCode"]"
                          Variant="Variant.Outlined" Clearable Class="search-com" />
        </MudItem>
        <MudItem xs="12" sm="6" md="3" lg="2" xl="1">
            <MudTextField T="string" @bind-Value="searchObject.SearchModelName"
                          Margin="Margin.Dense" Label="@Loc["AIConfigPage_SearchModel"]"
                          Variant="Variant.Outlined" Clearable Class="search-com" />
        </MudItem>
        <MudItem xs="12" sm="6" md="3" lg="2" xl="1">
            <MudTextField T="string" @bind-Value="searchObject.SearchEndpoint"
                          Margin="Margin.Dense" Label="@Loc["AIConfigPage_SearchEndpoint"]"
                          Variant="Variant.Outlined" Clearable Class="search-com" />
        </MudItem>
        <MudItem>
            <MudIconButton Size="MudBlazor.Size.Medium" Icon="@Icons.Material.Filled.Search"
                           OnClick="()=>{ dataGrid.ReloadServerData(); }"
                           Variant="Variant.Outlined" Color="Color.Primary" Class="mr-2" />
            <MudTooltip Text="重置搜索" Color="Color.Primary">
                <MudIconButton Size="MudBlazor.Size.Medium" Icon="@Icons.Material.Filled.SearchOff"
                               OnClick="SearchReset" Variant="Variant.Outlined" Color="Color.Primary" />
            </MudTooltip>
            @if (_accessService.CheckHasElementRights("AIConfigAddBtn").Result)
            {
                <MudIconButton Size="MudBlazor.Size.Medium" Icon="@Icons.Material.Filled.Add" Class="ml-2"
                               OnClick="AddConfigClick" Variant="Variant.Outlined" Color="Color.Primary" />
            }
        </MudItem>
    </MudGrid>
</PageHeader>

<MudDataGrid Dense=PageDataGridConfig.Dense Filterable=PageDataGridConfig.Filterable ColumnResizeMode=PageDataGridConfig.ColumnResizeMode
             SortMode=PageDataGridConfig.SortMode Groupable=PageDataGridConfig.Groupable Virtualize=PageDataGridConfig.Virtualize
             FixedHeader=PageDataGridConfig.FixedHeader Elevation=PageDataGridConfig.Elevation Outlined=PageDataGridConfig.Outlined
             Style="@PageDataGridConfig.Style" HorizontalScrollbar="PageDataGridConfig.HorizontalScrollbar"
             ServerData="GetTableData" @ref="dataGrid" T="ConfigModel">
    <Columns>
        <PropertyColumn Property="x => x.Number" Title="@Loc["AIConfigPage_TableH1"]"
                        StickyLeft="true" Resizable="false" HeaderStyle="width:80px;" />
        <PropertyColumn Property="x => x.ConfigName" Title="@Loc["AIConfigPage_TableH7"]" />
        <PropertyColumn Property="x => x.ModelName" Title="@Loc["AIConfigPage_TableH2"]" />
        <PropertyColumn Property="x => x.Endpoint" Title="@Loc["AIConfigPage_TableH3"]" />
        <PropertyColumn Property="x => x.ContextLength" Title="@Loc["AIConfigPage_TableH4"]" />
        <PropertyColumn Property="x => x.Description" Title="@Loc["AIConfigPage_TableH5"]" />
        <TemplateColumn Title="@Loc["AIConfigPage_TableH6"]" HeaderStyle="width:130px;" StickyRight="true">
            <CellTemplate>
                <MudTooltip Text="@Loc["AIConfigPage_TestBtn"]" Color="Color.Primary">
                    <MudIconButton Size="Size.Small" Icon="@Icons.Material.Outlined.PlayCircle" Color="Color.Primary"
                                   OnClick="() => TestAi(context.Item.Id)" />
                </MudTooltip>
                @if (_accessService.CheckHasElementRights("AIConfigEditBtn").Result)
                {
                    <MudIconButton Size="Size.Small" Icon="@Icons.Material.Outlined.Edit" Color="Color.Primary"
                                   OnClick="() => EditConfigClick(context.Item.Id)" />
                }
                @if (_accessService.CheckHasElementRights("AIConfigDeleteBtn").Result)
                {
                    <MudIconButton Size="Size.Small" Icon="@Icons.Material.Outlined.Delete" Color="Color.Error"
                                   OnClick="() => DeleteConfigClick(context.Item.Id)" />
                }
            </CellTemplate>
        </TemplateColumn>
    </Columns>
</MudDataGrid>

<PagePagination PageInfo="searchObject" PageChangedClick="PageChangedClick" />
